package ssm.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ssm.dao.MyResult;
import ssm.service.PerceptionService;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Thread.sleep;

@Controller
@RequestMapping("/perception")
public class PerceptionController {


    @Autowired
    private PerceptionService perceptionService;

    @RequestMapping("/numberAndTree")
    @ResponseBody
    public MyResult getnumberAndTree( @RequestParam String time){
        Map<String,Object> result=perceptionService.getnumberAndTree(time);
        return  MyResult.ok(result);
    }


    @RequestMapping("/InSchoolTable")
    @ResponseBody
    public MyResult getInSchoolTable(@RequestParam String jid){
        Map<String,Map<String,Object>> result= null;
        try {
            result = perceptionService.getInSchoolTable(jid);
        } catch (ParseException e) {
            System.out.println("出现异常啦");
        }
        return MyResult.ok(result);
    }

    @RequestMapping("/InSchoolByClass")
    @ResponseBody
    public MyResult getInSchoolByClass(@RequestParam(value = "list" ,required = false) List<String> list,
                                         @RequestParam (value = "time") String time){
        List<Map<String, Object>> result=perceptionService.getInSchoolByClass(list,time);
        return MyResult.ok(result);
    }


    @RequestMapping("/InSchoolByTeacher")
    @ResponseBody
    public MyResult getInSchoolByTeacher(@RequestParam(value = "list" ,required = false) List<String> list,
                                         @RequestParam (value = "time") String time){

        Map<String,Object> result=perceptionService.getInSchoolByTeacher(list,time);
        return MyResult.ok(result);
    }


    @RequestMapping("/InSchoolByStudent")
    @ResponseBody
    public MyResult getInSchoolByStudent(@RequestParam(value="zy") String zy,
                                        @RequestParam(value = "bj") String bj,
                                         @RequestParam (value = "time") String time){
        List<Map<String,Object>> result=perceptionService.getInSchoolByStudent(zy,bj,time);
        return MyResult.ok(result);
    }


    @RequestMapping("/getOutSchoolPersonCount")
    @ResponseBody
    public MyResult getOutSchoolPersonCount(String time){
        Map<String,Object>result=perceptionService.getOutSchoolPersonCount(time);
        return MyResult.ok(result);
    }


    @RequestMapping("/getLast14DaysInfo")
    @ResponseBody
    public MyResult getLast14DaysInfo(String date){
        try {
            Map<String,Object> result = perceptionService.getLast14DaysInfo(date);
            return MyResult.ok(result);
        } catch (ParseException e) {
            return MyResult.except("参数异常","请检查代码或传参");
        }
    }


    @ResponseBody
    @RequestMapping("/getTeacherStatusInfo")
    public MyResult getTeacherStatusInfo(String date){
        Map<String,Object> result= null;
        try {
            result = perceptionService.getTeacherStatusInfo(date);
        } catch (ParseException e) {
            return MyResult.except("参数异常","请检查代码或传参");
        }
        return MyResult.ok(result);
    }


    @RequestMapping("/getKqByTeacher")
    @ResponseBody
    public MyResult getKqByTeacher(@RequestParam(value = "list" ,required = false) List<String> list,
                                         @RequestParam (value = "time") String time){

        Map<String,Object> result=perceptionService.getKqByTeacher(list,time);
        return MyResult.ok(result);
    }

    @Test
    public void createThread(){
        new Thread(()->{
            for(int i=1;i<21;i++){
                Long time=System.currentTimeMillis();
                while (true){
                    if(System.currentTimeMillis()-time>30000){
                        time=System.currentTimeMillis();
                        break;
                    }
                }
                System.out.println("ThreadName第"+i+"次："+Thread.currentThread().getName());

            }

        }).start();
    }

}
